<div v-cloak id="app" class="main-content">
    <one-table
        :option="option"
        :data="data"
        :loading="loading"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
    >
        <!-- 工具菜单 -->
        <template slot="menuLeft">
            <el-button type="primary" size="mini" @click="handleAdd">
                添加
            </el-button>
        </template>

        <template slot="status" slot-scope="scope">
            <el-switch v-model="scope.row.status" :disabled="scope.row.system" :active-value="1" :inactive-value="0"
                @change="changeSwitch(scope.row)">
        </template>

        <!-- 自定义操作列 -->
        <template slot="operates" slot-scope="scope">
            <el-button
                size="mini"
                class="_tool"
                @click="handleEdit(scope.row, scope.index)"
            >
                <i class="el-icon-edit"></i>
            </el-button>
            <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="删除不能恢复,确定删除吗？"
                @confirm="handleDel(scope.row, scope.index)"
            >
                <el-button
                    :disabled="scope.row.id==1"
                    slot="reference"
                    size="mini"
                    class="_tool"
                    type="danger"
                >
                    <i class="el-icon-delete"></i>
                </el-button>
            </el-popconfirm>
        </template>
    </one-table>
</div>
{include file="common@components/one-table"}
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                loading: false,
                data: [],
                option: {
                    index: true,
                    header: true,
                    selection: false,
                    operates: { width: 100 },
                    page: {},
                    size: "mini",
                    column: [
                        {
                            label: "状态",
                            prop: "status",
                            slot: true,
                        },
                        {
                            label: "用户帐号",
                            prop: "username",
                        },
                        {
                            label: "域名",
                            prop: "domain",
                        },
                        {
                            label: "QQ号",
                            prop: "qq",
                        },
                        {
                            label: "站点版本",
                            prop: "version",
                        },
                        {
                            label: "站点标识",
                            prop: "identifier",
                        },
                        {
                            label: "创建时间",
                            prop: "create_time",
                        },
                        {
                            label: "更新时间",
                            prop: "update_time",
                        },
                    ],
                },
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 获取数据
            getData() {
                let _this = this;
                _this.loading = true;
                request({
                    params: {
                        s: "upgrade/bind/index",
                        page: _this.option.page.currentPage,
                        limit: _this.option.page.pageSize,
                    },
                    method: "post",
                }).then((e) => {
                    _this.loading = false;
                    _this.data = e.data.data.list;
                    _this.option.page.total = e.data.data.count;
                });
            },
            //分页
            handleCurrentChange(currentPage) {
                let _this = this;
                _this.option.page.currentPage = currentPage;
                _this.getData();
            },
            //每页显示数
            handleSizeChange(val) {
                let _this = this;
                _this.option.page.pageSize = val;
                _this.getData();
            },
            //改变状态
            changeSwitch(row) {
                let _this = this;
                _this.loading = true;
                request({
                    params: {
                        s: "upgrade/bind/status/",
                        id: row.id,
                        val: row.status,
                    },
                    method: "post",
                }).then((e) => {
                    _this.loading = false;
                });
            },

            //删除行
            handleDel(row, index) {
                let _this = this;
                _this.loading = true;
                request({
                    params: {
                        s: "upgrade/bind/del/",
                        id: row.id,
                    },
                    method: "post",
                }).then((e) => {
                    _this.loading = false;
                    e.data.code == 0 && _this.data.splice(index, 1);
                });
            },
            //添加
            handleAdd() {
                let _this = this;
                navigateTo({
                    s: "upgrade/bind/edit",
                });
            },
            //编辑
            handleEdit(row, index) {
                let _this = this;
                navigateTo({
                    s: "upgrade/bind/edit",
                    id: row.id,
                });
            },
        },
    });
</script>
